package com.ruoyi.business.service.impl;

import java.util.ArrayList;
import java.util.List;

import com.alibaba.fastjson.JSON;
import com.ruoyi.business.domain.HistoricActivity;
import com.ruoyi.business.domain.ServiceItem;
import com.ruoyi.business.service.IProcessService;
import com.ruoyi.business.service.IServiceItemService;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.exception.BusinessException;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.ShiroUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.system.service.ISysUserService;
import org.activiti.engine.history.HistoricActivityInstance;
import org.activiti.engine.task.Task;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.business.mapper.CarPackageAuditMapper;
import com.ruoyi.business.domain.CarPackageAudit;
import com.ruoyi.business.service.ICarPackageAuditService;
import com.ruoyi.common.core.text.Convert;
import org.springframework.util.CollectionUtils;

/**
 * 审核列表Service业务层处理
 * 
 * @author zw
 * @date 2021-10-17
 */
@Service
public class CarPackageAuditServiceImpl implements ICarPackageAuditService 
{
    @Autowired
    private CarPackageAuditMapper carPackageAuditMapper;
    @Autowired
    private IProcessService processService;
    @Autowired
    private IServiceItemService serviceItemService;
    @Autowired
    private ISysUserService sysUserService;
    /**
     * 查询审核列表
     * 
     * @param id 审核列表ID
     * @return 审核列表
     */
    @Override
    public CarPackageAudit selectCarPackageAuditById(Long id)
    {
        return carPackageAuditMapper.selectCarPackageAuditById(id);
    }

    /**
     * 查询审核列表列表
     * 
     * @param carPackageAudit 审核列表
     * @return 审核列表
     */
    @Override
    public List<CarPackageAudit> selectCarPackageAuditList(CarPackageAudit carPackageAudit)
    {
        carPackageAudit.setCreateBy(ShiroUtils.getSysUser().getUserId().toString());
        List<CarPackageAudit> carPackageAudits = carPackageAuditMapper.selectCarPackageAuditList(carPackageAudit);
        for (CarPackageAudit packageAudit : carPackageAudits) {
            List<Task> taskList = processService.getTaskByInstanceId(packageAudit.getInstanceId());
            if (!CollectionUtils.isEmpty(taskList)) {
                Task task = taskList.get(0);
                packageAudit.setTaskId(task.getId());
                packageAudit.setTaskName(task.getName());
            } else {
                packageAudit.setTaskName("已结束");
            }
        }
        return carPackageAudits;
    }

    /**
     * 新增审核列表
     * 
     * @param carPackageAudit 审核列表
     * @return 结果
     */
    @Override
    public int insertCarPackageAudit(CarPackageAudit carPackageAudit)
    {
        carPackageAudit.setCreateTime(DateUtils.getNowDate());
        return carPackageAuditMapper.insertCarPackageAudit(carPackageAudit);
    }

    /**
     * 修改审核列表
     * 
     * @param carPackageAudit 审核列表
     * @return 结果
     */
    @Override
    public int updateCarPackageAudit(CarPackageAudit carPackageAudit)
    {
        return carPackageAuditMapper.updateCarPackageAudit(carPackageAudit);
    }

    /**
     * 删除审核列表对象
     * 
     * @param ids 需要删除的数据ID
     * @return 结果
     */
    @Override
    public int deleteCarPackageAuditByIds(String ids)
    {
        return carPackageAuditMapper.deleteCarPackageAuditByIds(Convert.toStrArray(ids));
    }

    /**
     * 删除审核列表信息
     * 
     * @param id 审核列表ID
     * @return 结果
     */
    @Override
    public int deleteCarPackageAuditById(Long id)
    {
        return carPackageAuditMapper.deleteCarPackageAuditById(id);
    }

    @Override
    public void cancelApply(Long auditId, String instanceId) {
        CarPackageAudit carPackageAudit = carPackageAuditMapper.selectCarPackageAuditById(auditId);
        if (carPackageAudit == null){
            throw new BusinessException("审核记录不存在");
        }
        if (!ShiroUtils.getSysUser().getUserId().toString().equals(carPackageAudit.getCreateBy())){
            throw new BusinessException("你没有权限撤销别人的审核");
        }
        if (!CarPackageAudit.STATUS_IN_ROGRESS.equals(carPackageAudit.getStatus())){
            throw new BusinessException("只有审核中才能撤销审核");
        }
        //修改审核记录相关信息(状态,当前审核人)
        carPackageAudit.setStatus(CarPackageAudit.STATUS_CANCEL);
        carPackageAudit.setAuditors("");
        carPackageAuditMapper.updateCarPackageAudit(carPackageAudit);
        //修改服务项的状态为初始化
        ServiceItem serviceItem = carPackageAudit.getServiceItem();
        serviceItemService.updateAuditStatus(serviceItem.getId(), ServiceItem.AUDITSTATUS_AUDITING, ServiceItem.AUDITSTATUS_INIT);
        //访问activiti的API将流程实例删除(设置取消)
        processService.deleteProcessInstance(instanceId,ShiroUtils.getSysUser().getUserName() +  "撤销了审核");
    }

    @Override
    public List<HistoricActivity> selectHistoryList(String instanceId) {
        //根据指定的实例Id查询历史活动列表
        List<HistoricActivityInstance> historicActivityInstances = processService.selectHistoryList(instanceId);
        List<HistoricActivity> historicActivities = new ArrayList<>();
        //遍历将对象转换为HistoryActivity
        HistoricActivity activity = null;
        for (HistoricActivityInstance historicActivityInstance : historicActivityInstances) {
            activity = new HistoricActivity();
            //使用BeanUtils来属性拷贝
            BeanUtils.copyProperties(historicActivityInstance, activity);
            //查询分配人姓名设置到活动对象中
            String assignee = activity.getAssignee();
            if (StringUtils.isNotEmpty(assignee)) {
                SysUser sysUser = sysUserService.selectUserById(Long.parseLong(assignee));
                activity.setAssignee(sysUser.getUserName());
            }
            //查询批注放到活动对象中
            String comment = processService.selectHistoryTaskCommentByTaskId(activity.getTaskId());
            activity.setComment(comment);
            //将任务对象加到列表中
            historicActivities.add(activity);
        }

        return historicActivities;
    }
}
